<template>
  <div class="login-container h-screen w-full flex flex-1 flex-col justify-center items-center">
    <div class="login-main loc-center">
      <div class="login-main-bc-1"></div>
      <div class="login-main-bc-2"></div>
      <div class="login-main-bc-3"></div>
      <div class="login-main-bc-4"></div>
      <div class="login-main-box">
        <div class="l-m-b-content flex flex-col items-center justify-center">
          <el-card class="box-card l-m-b-c-card">
            <el-row style="justify-content: center">
              <h3 class="card-title text-center">{{ $t("setup.welcome") }}</h3>
            </el-row>
            <el-row style="padding: 32px 0 0">
              <el-form ref="loginForm" :model="loginForm" label-width="0">
                <el-form-item label="" prop="username">
                  <el-input clearable class="login-input" v-model="loginForm.username"
                    :placeholder="$t('setup.account')"></el-input>
                </el-form-item>
                <el-form-item label="" prop="password">
                  <el-input clearable class="login-input" v-model="loginForm.password"
                    :type="showPassword ? 'text' : 'password'" :placeholder="$t('setup.password')">
                    <span slot="suffix" role="img" class="appIcon">
                      <svg-icon :icon-class="showPassword ? 'eye-open' : 'eye-invisible'" width="16" height="16"
                        @click="handlePasswordVisible" />
                    </span>
                  </el-input>
                </el-form-item>
                <el-form-item prop="code" v-if="captchaEnabled">
                  <el-input clearable class="login-input" v-model="loginForm.code" auto-complete="off"
                    :placeholder="$t('setup.verificationCode')" style="width: 63%" @keyup.enter.native="handleLogin">
                  </el-input>
                  <div class="login-code">
                    <img :src="codeUrl" @click="getCode" class="login-code-img" />
                  </div>
                </el-form-item>
              </el-form>
            </el-row>
            <el-row>
              <el-button class="jtyj-primary-color login-btn" @click.native.prevent="handleLogin">
                <span v-if="!loading">{{ $t("setup.login") }}</span>
                <span v-else>{{ $t("setup.logining") }}</span></el-button>
            </el-row>
          </el-card>
        </div>
      </div>
      <div class="system-style-set">
        <div class="language-change">
          <span role="img" class="appIcon">
            <svg-icon class="theme-text-color" icon-class="language-index" width="22" height="22" />
          </span>
          <el-select v-model="curLanguage" class="jtyj-login-select" @change="handleChangeLanguage">
            <el-option v-for="item in languageOptions" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </div>
        <div class="appearance-btn" @click="dialogVisible = true">
          <span role="img" class="appIcon" style="margin-right: 5px">
            <svg-icon class="theme-text-color" icon-class="appearance" width="20" height="20" />
          </span>
          {{ $t("tool.modelManage.appearenceSetting") }}
        </div>
      </div>
    </div>
    <el-dialog title="" :visible.sync="dialogVisible" width="50vw" custom-class="jtyj-dialog-setup"
      :before-close="handleClose">
      <SetUp :isLogin="true" @handleClose="handleClose"></SetUp>
    </el-dialog>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";
import { mapState } from "vuex";
import SetUp from "./common-components/set-up-dialog.vue";

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      curLanguage: null,
      dialogVisible: false,
      showPassword: false,
      loginForm: {
        username: "admin",
        password: "admin123",
        rememberMe: false,
        code: "",
        uuid: "",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" },
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" },
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }],
      },
      loading: false,
      // 验证码开关
      captchaEnabled: false,
      // 注册开关
      register: false,
      redirect: undefined,
    };
  },
  components: {
    SetUp,
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  computed: {
    ...mapState({
      languageOptions: (state) => state.systemStyle.languageOptions,
    }),
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  mounted() {
    this.curLanguage = this.$i18n.locale;
  },
  methods: {
    getCode() {
      getCodeImg().then((res) => {
        this.captchaEnabled =
          res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get("rememberMe");
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password:
          password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), {
              expires: 30,
            });
            Cookies.set("rememberMe", this.loginForm.rememberMe, {
              expires: 30,
            });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove("rememberMe");
          }
          this.$store
            .dispatch("Login", this.loginForm)
            .then(() => {
              // this.redirect ||
              this.$router.push({ path: "/" }).catch(() => { });
            })
            .catch(() => {
              this.loading = false;
              if (this.captchaEnabled) {
                this.getCode();
              }
            });
        }
      });
    },
    handlePasswordVisible() {
      this.showPassword = !this.showPassword;

    },
    handleChangeLanguage(val) {
      localStorage.setItem("platformLanguage", val);
      location.reload(true);
    },
    // 控制设置弹框是否展示
    handleClose() {
      this.dialogVisible = false;
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss">
.appIcon {
  color: var(--ui-breadcrumb-base-color);
}

.login-container {
  position: relative;
  overflow: hidden;

  .login-main {
    position: relative;
    width: 100vw;
    height: 100vh;
    background: var(--ui-component-background);
    flex-direction: column;
    background-size: cover;
    background-position: center center;
    background-repeat: no-repeat;
    overflow: hidden;

    .login-main-bc-1 {
      width: 560px;
      height: 560px;
      opacity: 0.8;
    }

    .login-main-bc-2 {
      width: 680px;
      height: 680px;
      opacity: 0.6;
    }

    .login-main-bc-3 {
      width: 800px;
      height: 800px;
      opacity: 0.4;
    }

    .login-main-bc-4 {
      width: 920px;
      height: 920px;
      opacity: 0.2;
    }

    .login-main-bc-1,
    .login-main-bc-2,
    .login-main-bc-3,
    .login-main-bc-4 {
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
      border-radius: 50%;
      border: 1px solid var(--ui-border-color-base);
    }

    .login-main-box {
      -webkit-app-region: no-drag;

      .l-m-b-content {
        padding: 0 24px;
        margin: 0 auto;

        .l-m-b-c-card {
          width: 440px;
          border: 1px solid var(--ui-border-color-base);
          box-shadow: rgba(22, 14, 45, 0.02) 0px 0px 40px,
            rgba(22, 14, 45, 0.06) 0px 0px 104px;
          border-radius: 18px;

          .card-title {
            align-self: flex-start;
            color: var(--app-text-fg-high);
            margin-bottom: 8px;
            font-size: 30px;
          }
        }
      }
    }

    .system-style-set {
      position: absolute;
      left: 16px;
      bottom: 12px;
      display: flex;
      align-items: center;

      .language-change {
        display: flex;
        align-items: center;
        margin-right: 10px;
      }

      .appearance-btn {
        height: 32px;
        color: var(--ui-text-color);
        display: flex;
        align-items: center;
        user-select: none;
        padding: 4px 15px;
        border-radius: 8px;
        box-sizing: border-box;
        cursor: pointer;

        &:hover {
          background-color: var(--ui-item-hover-bg);
        }
      }
    }
  }
}

.login-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 40px;
  font-size: 16px;
  line-height: 22px;
  border-radius: 8px;
  margin-top: 16px;
  margin-bottom: 8px;
}

.login-code {
  width: 33%;
  height: 38px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.login-code-img {
  height: 38px;
}
</style>
<style lang="scss">
.login-input.el-input {
  .el-input__inner {
    height: 40px;
  }

  .el-input__suffix {
    right: 11px;
    display: flex;
    align-items: center;
  }

  .el-input__suffix-inner {
    display: flex;
    align-items: center;
    flex-direction: row-reverse;
  }
}

.jtyj-login-select {

  & .el-input__inner,
  & .el-input__inner:hover,
  & .el-input__inner:focus {
    width: 120px;
    border: none;
  }
}
</style>